package com.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.model.User;
import com.service.UserService;
import com.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin("*")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 添加用户接口
     */
    @PostMapping("/users")
    public Result addUser(@RequestBody User user) {
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        boolean result = userService.save(user);
        if (result){
            return new Result("200","添加成功",1,null);
        } else {
            return new Result("500","添加失败,请联系管理员询问原因",0,null);
        }
    }

    /**
     * 根据ID删除用户接口
     */
    @DeleteMapping("/users/{id}")
    public Result deleteUserById(@PathVariable Integer id) {
        boolean result = userService.removeById(id);
        if (result){
            return new Result("200","删除成功",1,null);
        } else {
            return new Result("500","删除失败",0,null);
        }
    }

    /**
     * 根据用户 ID 查询用户
     * @param id 用户 ID
     * @return 返回查询到的用户对象
     */
    @GetMapping("/users/{id}")
    public Result getUserById(@PathVariable("id") Integer id) {
        User user = userService.getOne(new QueryWrapper<User>().eq("id", id));
        return new Result("200","查询成功",1, List.of(user));
    }

    /**
     * 根据ID或UserName或Account或Phone查询单个用户接口
     */
/*    @GetMapping("/users/{idOrUserNameOrAccountOrPhone}")
    public Result getUserByIdOrUserNameOrAccountOrPhone(@PathVariable String idOrUserNameOrAccountOrPhone) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.and(i -> i.eq("id", idOrUserNameOrAccountOrPhone)
                .or()
                .eq("user_name", idOrUserNameOrAccountOrPhone)
                .or()
                .eq("account", idOrUserNameOrAccountOrPhone)
                .or()
                .eq("phone", idOrUserNameOrAccountOrPhone));
        User user = userService.getOne(wrapper);
        return new Result("200","查询成功",1, List.of(user));
    }*/

    /**
     * 查询全部用户接口
     */
    @GetMapping("/users")
    public Result getAllUsers() {
        List<User> list = userService.list();
        return new Result("200","查询成功",list.size(),list);
    }

    /**
     * 分页查询用户接口
     */
    @GetMapping("/users/page/{pageNum}/{pageSize}")
    public IPage<User> getUsersByPage(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        return userService.page(new Page<>(pageNum, pageSize), null);
    }

    /**
     * 根据ID更新用户接口
     */
    @PutMapping("/users/{id}")
    public Result updateUserById(@PathVariable Integer id, @RequestBody User user) {
        user.setId(id);
        user.setUpdateTime(new Date());
        boolean result = userService.updateById(user);
        if (result){
            return new Result("200","更新成功",1,List.of(user));
        } else {
            return new Result("500","更新失败",0,null);
        }
    }

    /**
     * 根据phone修改password接口
     */
    @PutMapping("/users/phone/{phone}")
    public Result updatePasswordByPhone(@PathVariable String phone, @RequestBody User user) {
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("phone", phone).set("password", user.getPassword());
        boolean result = userService.update(wrapper);
        if (result){
            return new Result("200","更新密码成功",1,null);
        } else {
            return new Result("500","更新密码失败",0,null);
        }
    }

}

